<?php 
class Passport_model  extends CI_Model {

	function __construct(){
        parent::__construct();
    }

	function checkEmail($str){
		if(strlen($str) >50){
			return array(
				'ret'=>1,
				'msg'=>$this->Resultmessage_model->getMessage('EMAIL_TOO_LONG'),
			);
		}
		if(!preg_match("/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/",$str)){
			return array(
				'ret'=>2,
				'msg'=>$this->Resultmessage_model->getMessage('INVALID_EMAIL'),
			);
		}
		return array('ret'=>0, 'msg'=>'');
	}
	
	function checkPwd($str){
		if(strlen($str) < 6){
			return array(
				'ret'=>1,
				'msg'=>$this->Resultmessage_model->getMessage('PWD_TOO_SHORT'),
			);
		}
		if(strlen($str) > 16){
			return array(
				'ret'=>2,
				'msg'=>$this->Resultmessage_model->getMessage('PWD_TOO_LONG'),
			);
		}
		
		if(!preg_match("/^[-_0123456789A-z]+$/",$str)){
			return array(
				'ret'=>3,
				'msg'=>$this->Resultmessage_model->getMessage('PWD_TOO_SIMPLE'),
			);
		}
		return array('ret'=>0, 'msg'=>'');
	}
	
	function checkMobile($str){
		if(empty($str)){
			return array('ret'=>0, 'msg'=>''); 
		}
		if(!preg_match("/^((852\d{8})|(((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15\d{9})|(18\d{9}))))$/", $str)){
			return array(
				'ret'=>1,
				'msg'=>$this->Resultmessage_model->getMessage('MOBILE_INVALID'),
			);
		}
		return array('ret'=>0, 'msg'=>'');
	}
	
    public function register($user){
    	$result = $this->checkEmail($user['email']); 
    	if($result['ret'] != 0){  
			return array(
				'ret'=>1,
				'email_error'=>$result['msg'], 
			);
    	}

    	$result =$this->checkPwd($user['password']); 
		if($result['ret'] != 0){
			return array(
				'ret'=>2,
				'pwd_error'=>$result['msg'], 
			);
		}
		
		if($user['password'] != $user['confimPwd']){
			return array(
				'ret'=>2,
				'pwd_error'=>$this->Resultmessage_model->getMessage('CONFIRM_PWD_MISMATCHED'), 
			);
		}
		unset($user['confimPwd']);
    	
		$result = $this->checkMobile($user['mobile']); 
		if($result['ret'] != 0){
			return array(
				'ret'=>3,
				'pwd_error'=>$result['msg'], 
			);
		}
		
		if(strlen($user['company']) > 50){
			return array(
				'ret'=>4,
				'pwd_error'=>$this->Resultmessage_model->getMessage('COMPANY_NAME_TOO_LONG'),
			);
		}
		
		if(strlen($user['company_url']) > 200){
			return array(
				'ret'=>5,
				'pwd_error'=>$this->Resultmessage_model->getMessage('COMPANY_URL_TOO_LONG'),
			);
		}
		
		$this->load->model('users/User_model');
		$userResult = $this->User_model->findUser($user['email']);
		if(!empty($userResult)){
			return array(
				'ret'=>6,
				'pwd_error'=>$this->Resultmessage_model->getMessage('USER_EXIST'),
			);
		}
		
    	$this->load->model('users/Password_model');
		$user['password'] = $this->Password_model->hash($user['password']);
		$user['disabled'] = 'false';
		
		
		$this->User_model->add($user);
		$user = $this->User_model->findUser($user['email']);
		
		$this->load->model('common/Usersession_model');
		$this->Usersession_model->setUser($user);
		
		return array('ret'=>0, 'msg'=>'');
    }
    
	public function login($user){
		$this->load->model('users/User_model');
		$userResult = $this->User_model->findUser($user['email']);
		if(empty($userResult)){
			return array(
				'ret'=>1,
				'msg'=>$this->Resultmessage_model->getMessage('USER_INVALID'),
			);
		}
		
		$this->load->model('users/Password_model');
		$isValid = $this->Password_model->validate($user['pwd'], $userResult['password']);
		if(!$isValid){
			return array(
				'ret'=>2,
				'msg'=>$this->Resultmessage_model->getMessage('PASSWORD_ERROR'),
			);
		}
		
		$this->load->model('common/Usersession_model');
		$this->Usersession_model->setUser($userResult);
		
		return array(
			'ret'=>0,
			'msg'=>$this->Resultmessage_model->getMessage('LOGIN_SUCCEED'),
		);
	}

	public function logout(){
		$this->load->model('common/Usersession_model');
		$this->Usersession_model->destroy();
	}
}